package com.theatre.userservice.Controller;

import com.theatre.userservice.annotation.RequirePermission;
import com.theatre.userservice.model.entity.Cart;
import com.theatre.userservice.model.vo.CartVO;
import com.theatre.userservice.model.vo.Result;
import com.theatre.userservice.service.CartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/cart")
@Tag(name = "购物车接口", description = "提供购物车相关的接口")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 添加到购物车
     */
    @PostMapping("/add")
    @RequirePermission("content:manage")
    @Operation(summary = "添加到购物车")
    public Result<String> addToCart(@RequestBody Cart cart) {
        boolean success = cartService.addToCart(cart);
        return success ? Result.success("添加成功") : Result.error("添加失败");
    }

    /**
     * 删除购物车中的某项
     */
    @DeleteMapping("/{id}")
    @RequirePermission("content:manage")
    @Operation(summary = "删除购物车中的某项")
    public Result<String> removeFromCart(@PathVariable Long id) {
        boolean success = cartService.removeFromCart(id);
        return success ? Result.success("删除成功") : Result.error("删除失败");
    }

    /**
     * 查看当前商铺的购物车
     */
    @GetMapping("/list/{shopId}")
    @RequirePermission("content:manage")
    @Operation(summary = "查看当前商铺的购物车")
    public Result<List<CartVO>> listCart(@PathVariable Long shopId) {
        List<CartVO> list = cartService.listCartByShopId(shopId);
        return Result.success(list);
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/clear/{shopId}")
    @RequirePermission("content:manage")
    @Operation(summary = "清空购物车")
    public Result<String> clearCart(@PathVariable Long shopId) {
        boolean success = cartService.clearCart(shopId);
        return success ? Result.success("清空成功") : Result.error("清空失败");
    }

    /**
     * 结算购物车（购买）
     */
    @PostMapping("/checkout")
    @RequirePermission("content:manage")
    @Operation(summary = "结算购物车（购买）")
    public Result<Long> checkout(@RequestParam("shopId") Long shopId) {
        BigDecimal price = cartService.checkout(shopId);
        return Result.success(price.longValue());
    }

}
